package se.perp.c4.ui;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.image.BufferedImage;
import javax.swing.BorderFactory;

public class SpecialCardMiniature extends ImagePanel {

    private boolean crossedOut = false;
    private String tooltip;
    private Color normal = new Color(0, 0, 0, 64);
    private Color played = new Color(0, 0, 0, 168);

    public SpecialCardMiniature(SpecialCard toModel) {
        BufferedImage img = null;
        if (toModel instanceof Palantir) {
            img = Images.get(Images.Which.PALANTIR_SMALL);
        } else if (toModel instanceof Recall) {

            img = Images.get(Images.Which.RECALL_TO_MORDOR_SMALL);
        } else if (toModel instanceof Shadowfax) {
            img = Images.get(Images.Which.SHADOWFAX_SMALL);
        } else if (toModel instanceof GandalfWhite) {
            img = Images.get(Images.Which.GANDALF_WHITE_SMALL);
        }

        setHoverSensitive(false);
        StringBuffer sb = new StringBuffer(toModel.getEnabledTooltipText());
        int bodyEnd = sb.indexOf("<body>") + 6;
        sb.insert(bodyEnd, "<i>[Opponent special card]</i><br>");
        tooltip = sb.toString();
        setToolTipText(tooltip);
        setImage(img);

        setBorder(BorderFactory.createLineBorder(Color.BLACK));
    }

    public void setCrossedOut(boolean b) {
        crossedOut = b;
        if (crossedOut) {
            setToolTipText("Your opponent has already played this.");
        } else {
            setToolTipText(tooltip);
        }
    }

    @Override
    protected void paintComponent(Graphics g_) {
        super.paintComponent(g_);
        Graphics2D g = (Graphics2D)g_;
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        if (crossedOut) {
            g.setColor(played);
        } else {
            g.setColor(normal);
        }

        g.fillRect(0, 0, getWidth(), getHeight());

        if (crossedOut) {
            Graphics2D g2 = (Graphics2D) g;
            g2.setColor(Color.RED);
            Stroke oldStroke = g2.getStroke();
            g2.setStroke(new BasicStroke(3.0f));
            g2.drawLine(5, 5, getWidth() - 6, getHeight() - 6);
            g2.drawLine(getWidth() - 6, 5, 5, getHeight() - 6);
            g2.setStroke(oldStroke);
        }
    }
}
